﻿using System.ComponentModel.DataAnnotations.Schema;
using CRM.Extensions;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json.Linq;

namespace CRM.Models.GlobalSearch
{
    [Index(nameof(ApiNameHashCode), nameof(ParamsJsonHashCode))]
    [Index(nameof(CreatedAt))]
    [Index(nameof(ApiNameHashCode), nameof(CreatedAt))]
    public class ApiInvokedLog : BaseModel
    {
        private string _apiName;
        private string _paramsJson;
        public int Id { get; set; }

        [Comment("调用接口方法名称")]
        public string ApiName
        {
            get => _apiName;
            set
            {
                _apiName = value;
                ApiNameHashCode = value.GetFixedHashCode();
            }
        }

        public long ApiNameHashCode { get; set; }

        [Comment("请求参数")]
        public string ParamsJson
        {
            get => _paramsJson;
            set
            {
                _paramsJson = value;
                ParamsJsonHashCode = value.GetFixedHashCode();
            }
        }

        public long ParamsJsonHashCode { get; private set; }

        
        
        [Comment("响应结果")]
        public string ResponseJson { get; set; }

        [NotMapped]
        public JObject Response
        {
            get => JObject.Parse(ResponseJson);
            set => ResponseJson = value.ToString();
        }

        public int UserId { get; set; }
        public int GroupId { get; set; }
        public int CompanyId { get; set; }
        public int OemId { get; set; }
        public int EmailMarketingId { get; set; } = 0;
    }
}